<!DOCTYPE html>
<!--
Copyright (C) 2017 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-project</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-project.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <gr-project></gr-project>
  </template>
</test-fixture>

<script>
  suite('gr-project tests', () => {
    let element;
    let sandbox;
    const PROJECT = 'test-project';
    const SCHEMES = {http: {}, repo: {}, ssh: {}};

    function getFormFields() {
      const selects = Polymer.dom(element.root).querySelectorAll('select');
      const textareas =
          Polymer.dom(element.root).querySelectorAll('iron-autogrow-textarea');
      const inputs = Polymer.dom(element.root).querySelectorAll('input');
      return inputs.concat(textareas).concat(selects);
    }

    setup(() => {
      sandbox = sinon.sandbox.create();
      stub('gr-rest-api-interface', {
        getLoggedIn() { return Promise.resolve(false); },
        getProjectConfig() {
          return Promise.resolve({
            description: 'Access inherited by all other projects.',
            use_contributor_agreements: {
              value: false,
              configured_value: 'FALSE',
            },
            use_content_merge: {
              value: false,
              configured_value: 'FALSE',
            },
            use_signed_off_by: {
              value: false,
              configured_value: 'FALSE',
            },
            create_new_change_for_all_not_in_target: {
              value: false,
              configured_value: 'FALSE',
            },
            require_change_id: {
              value: false,
              configured_value: 'FALSE',
            },
            reject_implicit_merges: {
              value: false,
              configured_value: 'FALSE',
            },
            private_by_default: {
              value: false,
              configured_value: 'FALSE',
            },
            match_author_to_committer_date: {
              value: false,
              configured_value: 'FALSE',
            },
            max_object_size_limit: {},
            submit_type: 'MERGE_IF_NECESSARY',
          });
        },
        getConfig() {
          return Promise.resolve({download: {}});
        },
      });
      element = fixture('basic');
    });

    teardown(() => {
      sandbox.restore();
    });

    test('loading displays before project config is loaded', () => {
      assert.isTrue(element.$.loading.classList.contains('loading'));
      assert.isFalse(getComputedStyle(element.$.loading).display === 'none');
      assert.isTrue(element.$.loadedContent.classList.contains('loading'));
      assert.isTrue(getComputedStyle(element.$.loadedContent)
          .display === 'none');
    });

    test('download commands visibility', () => {
      element._loading = false;
      flushAsynchronousOperations();
      assert.isTrue(element.$.downloadContent.classList
          .contains('hideDownload'));
      assert.isTrue(getComputedStyle(element.$.downloadContent)
          .display == 'none');
      element._schemesObj = SCHEMES;
      flushAsynchronousOperations();
      assert.isFalse(element.$.downloadContent.classList
          .contains('hideDownload'));
      assert.isFalse(getComputedStyle(element.$.downloadContent)
          .display == 'none');
    });

    test('form defaults to read only', () => {
      assert.isTrue(element._readOnly);
    });

    test('form defaults to read only when not logged in', done => {
      element.project = PROJECT;
      element._loadProject().then(() => {
        assert.isTrue(element._readOnly);
        done();
      });
    });

    test('form defaults to read only when logged in and not admin', done => {
      element.project = PROJECT;
      sandbox.stub(element, '_getLoggedIn', () => {
        return Promise.resolve(true);
      });
      sandbox.stub(element.$.restAPI, 'getProjectAccess', () => {
        return Promise.resolve({'test-project': {}});
      });
      element._loadProject().then(() => {
        assert.isTrue(element._readOnly);
        done();
      });
    });

    test('all form elements are disabled when not admin', done => {
      element.project = PROJECT;
      element._loadProject().then(() => {
        flushAsynchronousOperations();
        const formFields = getFormFields();
        for (const field of formFields) {
          assert.isTrue(field.hasAttribute('disabled'));
        }
        done();
      });
    });

    test('_formatBooleanSelect', () => {
      let item = {inherited_value: 'true'};
      assert.deepEqual(element._formatBooleanSelect(item), [
        {
          label: 'Inherit (true)',
          value: 'INHERIT',
        },
        {
          label: 'True',
          value: 'TRUE',
        }, {
          label: 'False',
          value: 'FALSE',
        },
      ]);

      // For items without inherited values
      item = {};
      assert.deepEqual(element._formatBooleanSelect(item), [
        {
          label: 'Inherit',
          value: 'INHERIT',
        },
        {
          label: 'True',
          value: 'TRUE',
        }, {
          label: 'False',
          value: 'FALSE',
        },
      ]);
    });

    suite('admin', () => {
      setup(() => {
        element.project = PROJECT;
        sandbox.stub(element, '_getLoggedIn', () => {
          return Promise.resolve(true);
        });
        sandbox.stub(element.$.restAPI, 'getProjectAccess', () => {
          return Promise.resolve({'test-project': {is_owner: true}});
        });
      });

      test('all form elements are enabled', done => {
        element._loadProject().then(() => {
          flushAsynchronousOperations();
          const formFields = getFormFields();
          for (const field of formFields) {
            assert.isFalse(field.hasAttribute('disabled'));
          }
          assert.isFalse(element._loading);
          done();
        });
      });

      test('state gets set correctly', done => {
        element._loadProject().then(() => {
          assert.equal(element._projectConfig.state, 'ACTIVE');
          done();
        });
      });

      test('fields update and save correctly', done => {
        const configInputObj = {
          description: 'new description',
          use_contributor_agreements: 'TRUE',
          use_content_merge: 'TRUE',
          use_signed_off_by: 'TRUE',
          create_new_change_for_all_not_in_target: 'TRUE',
          require_change_id: 'TRUE',
          reject_implicit_merges: 'TRUE',
          private_by_default: 'TRUE',
          match_author_to_committer_date: 'TRUE',
          max_object_size_limit: 10,
          submit_type: 'FAST_FORWARD_ONLY',
          state: 'READ_ONLY',
        };

        const saveStub = sandbox.stub(element.$.restAPI, 'saveProjectConfig'
            , () => {
              return Promise.resolve({});
            });

        const button = Polymer.dom(element.root).querySelector('gr-button');

        element._loadProject().then(() => {
          assert.isTrue(button.hasAttribute('disabled'));
          assert.isFalse(element.$.Title.classList.contains('edited'));
          element.$.descriptionInput.bindValue = configInputObj.description;
          element.$.stateSelect.bindValue = configInputObj.state;
          element.$.submitTypeSelect.bindValue = configInputObj.submit_type;
          element.$.contentMergeSelect.bindValue =
              configInputObj.use_content_merge;
          element.$.newChangeSelect.bindValue =
              configInputObj.create_new_change_for_all_not_in_target;
          element.$.requireChangeIdSelect.bindValue =
              configInputObj.require_change_id;
          element.$.rejectImplicitMergesSelect.bindValue =
              configInputObj.reject_implicit_merges;
          element.$.setAllnewChangesPrivateByDefaultSelect.bindValue =
              configInputObj.private_by_default;
          element.$.matchAuthoredDateWithCommitterDateSelect.bindValue =
              configInputObj.match_author_to_committer_date;
          element.$.maxGitObjSizeInput.bindValue =
              configInputObj.max_object_size_limit;
          element.$.contributorAgreementSelect.bindValue =
              configInputObj.use_contributor_agreements;
          element.$.useSignedOffBySelect.bindValue =
              configInputObj.use_signed_off_by;

          assert.isFalse(button.hasAttribute('disabled'));
          assert.isTrue(element.$.configurations.classList.contains('edited'));

          const formattedObj =
              element._formatProjectConfigForSave(element._projectConfig);
          assert.deepEqual(formattedObj, configInputObj);

          element._handleSaveProjectConfig().then(() => {
            assert.isTrue(button.hasAttribute('disabled'));
            assert.isFalse(element.$.Title.classList.contains('edited'));
            assert.isTrue(saveStub.lastCall.calledWithExactly(PROJECT,
                configInputObj));
            done();
          });
        });
      });
    });
  });
</script>
